package top.byfree.jbyan.saber.service

import org.apache.commons.mail.DefaultAuthenticator
import org.apache.commons.mail.EmailException
import org.apache.commons.mail.SimpleEmail
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import top.byfree.jbyan.saber.exception.SaberServiceException
import java.util.*

@Service
class EmailService(
    @Value("\${saber.email.host-name}") private val hostName: String,
    @Value("\${saber.email.send-email}") private val sendEmail: String,
    @Value("\${saber.email.auth-code}") private val authCode: String,
    @Value("\${saber.email.title}") private val title: String,
    @Value("\${saber.email.topic}") private val topic: String,
    @Value("\${saber.email.expiration}") private val expiration: String
) {

    fun sendEmail(targetEmail: String, code: String) {
        val email = SimpleEmail()
        //设置主机名,QQ邮箱是"smtp.qq.com",网易邮箱是"smtp.163.com"
        email.hostName = hostName
        // 用户名和密码为邮箱的账号和授权码（不需要进行base64编码）
        email.setAuthenticator(DefaultAuthenticator(sendEmail, authCode))
        //设置SSL连接,这样写就对了
        email.isSSLOnConnect = true
        //设置来源,就是发送方的邮箱地址
        try {
            email.setFrom(sendEmail, title)
            //设置主题,可以不设置
            email.setSubject(topic)

            val template = """
                <!DOCTYPE html>
                <html lang="zh-CN">
                <head>
                  <meta charset="UTF-8">
                  <meta name="viewport" content="width=device-width, initial-scale=1.0">
                  <title>邮箱验证码</title>
                  <style>
                    body {
                      font-family: sans-serif;
                      margin: 0 !important;
                      padding: 20px;
                      background-color: #fff;
                      color: #333;
                    }
                    h1 {
                      font-size: 24px;
                      margin-top: 0;
                      margin-bottom: 10px;
                    }
                    p {
                      margin-bottom: 10px;
                    }
                    .code {
                      font-size: 48px;
                      font-weight: bold;
                      margin: 10px 0;
                      padding: 10px;
                      border: 1px solid #ddd;
                      border-radius: 5px;
                      text-align: center;
                    }
                  </style>
                </head>
                <body>
                  <div style="text-align: center;">
                    <h1>邮箱验证码</h1>
                    <p>您好，${targetEmail}，</p>
                    <p>您正在注册Saber-Admin账户，您的邮箱验证码是：</p>
                    <h1 class="code">${code}</h1>
                    <p>请在${expiration}分钟内输入验证码完成验证。</p>
                    <p>如果您没有注册Saber-Admin账户，请忽略此邮件。</p>
                  </div>
                </body>
                </html>
            """.trimIndent()

            //设置信息,就是内容,这个必须要有
            email.setMsg(template)
            //接收人邮箱地址
            email.addTo(targetEmail)
            email.send()
        } catch (e: EmailException) {
            val saberException = SaberServiceException("邮件发送失败")
            saberException.initCause(e)
            throw saberException
        }
    }

}
